#remotes::install_github("datalorax/edld652")
library(edld652)


#install.packages("usethis")
#usethis::edit_r_environ()
#list_datasets()

#unlink(file.path("~",".Renviron"))
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.0.2     ✓ forcats 0.5.1
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(stringr)
library(here)
## here() starts at /Users/dwelindt/Documents/Classes/EDLD 652/Lab1/dataviz-psy-group/Final Project
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.1, GDAL 3.4.0, PROJ 8.1.1; sf_use_s2() is TRUE
datasets <- data.frame(names = edld652::list_datasets())
fiscal_names <- datasets %>% filter(str_detect(names, "CCD_fiscal"))

dis_dat <- get_data("NCES_CCD_nonfiscal_district_2017_2021_disabilities")
## Rows: 73044 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (12): DMS_FLAG, FIPST, FILEURL, LEAID, LEA_NAME, SCHOOL_YEAR, ST, STATE...
## dbl   (2): IDEA_COUNT, YEAR
## dttm  (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#fisc_dat <-  lapply(fiscal_names, get_data) # apparently not all of the fiscal datasets have the same number of variables...
#fisc_dat2 <- do.call(rbind, fisc_2)

raw_fisc_2017 <- get_data("NCES_CCD_fiscal_district_2017") # 2017 should be a good proxy for what we care about
## Rows: 18680 Columns: 264── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (131): AGCHRT, CBSA, CENSUSID, CONUM, CSA, FIPST, FL_19H, FL_21F, FL_31...
## dbl  (132): A07, A08, A09, A11, A13, A15, A20, A40, B10, B11, B12, B13, C01,...
## dttm   (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
raw_fisc_2018 <- get_data("NCES_CCD_fiscal_district_2018") 
## Rows: 18715 Columns: 266── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (132): AGCHRT, CBSA, CENSUSID, CONUM, CSA, FIPST, FL_19H, FL_21F, FL_31...
## dbl  (133): A07, A08, A09, A11, A13, A15, A20, A40, B10, B11, B12, B13, C01,...
## dttm   (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
raw_fisc_2018 <- raw_fisc_2018 %>% 
  select(-CE3, -FL_CE3)
fiscal <- rbind(raw_fisc_2017, raw_fisc_2018)


raw_fisc_2018 <- get_data("NCES_CCD_fiscal_district_2018") 
## Rows: 18715 Columns: 266── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (132): AGCHRT, CBSA, CENSUSID, CONUM, CSA, FIPST, FL_19H, FL_21F, FL_31...
## dbl  (133): A07, A08, A09, A11, A13, A15, A20, A40, B10, B11, B12, B13, C01,...
## dttm   (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
raw_fisc_2018 <- get_data("NCES_CCD_fiscal_district_2018") 
## Rows: 18715 Columns: 266── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (132): AGCHRT, CBSA, CENSUSID, CONUM, CSA, FIPST, FL_19H, FL_21F, FL_31...
## dbl  (133): A07, A08, A09, A11, A13, A15, A20, A40, B10, B11, B12, B13, C01,...
## dttm   (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
state_info <- readr::read_csv("https://github.com/kjhealy/fips-codes/raw/master/state_fips_master.csv")
## Rows: 50 Columns: 10── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): state_name, state_abbr, long_name, region_name, division_name
## dbl (5): fips, sumlev, region, division, state
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
acgr_lea <- get_data("EDFacts_acgr_lea_2011_2019")
## Rows: 11326 Columns: 29── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (17): ALL_RATE, CWD_RATE, DATE_CUR, ECD_RATE, FIPST, FILEURL, LEAID, LE...
## dbl  (11): ALL_COHORT, CWD_COHORT, ECD_COHORT, LEP_COHORT, MAM_COHORT, MAS_C...
## dttm  (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
acgr_sch <- get_data("EDFacts_acgr_sch_2011_2019")
## Rows: 21335 Columns: 31── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (29): ALL_COHORT, ALL_RATE, CWD_COHORT, CWD_RATE, DATE_CUR, ECD_COHORT,...
## dbl   (1): YEAR
## dttm  (1): DL_INGESTION_DATETIME
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Revenue By State

raw_fiscal_17 <- raw_fisc_2017 %>% 
  select(FIPST, LEAID, TOTALREV) %>% 
  mutate(fips = as.numeric(FIPST))


fiscal_sub <- fiscal %>% 
  select(FIPST, LEAID, TOTALREV, sped_rev_st = C05, idea_rev_fed = C15) %>% 
  mutate(fips = as.numeric(FIPST))

state_fiscal_17 <- full_join(raw_fiscal_17, state_info)
## Joining, by = "fips"
state_fiscal <- full_join(fiscal_sub, state_info)
## Joining, by = "fips"
state_fisc_sums <- state_fiscal %>% 
  group_by(state_name) %>% 
  summarize(min_idea_rev_fed = min(idea_rev_fed), 
            max_idea_rev_fed = max(idea_rev_fed), 
            mean_idea_rev_fed = mean(idea_rev_fed), 
            median_idea_rev_fed = median(idea_rev_fed), 
            min_sped_rev_st = min(sped_rev_st), 
            max_sped_rev_st = max(sped_rev_st), 
            mean_sped_rev_st = mean(sped_rev_st), 
            median_sped_rev_st = median(sped_rev_st) 
            ) 



state_fisc_sums %>% 
  ggplot(.,aes(fct_reorder(state_name, mean_sped_rev_st), mean_sped_rev_st))+
  geom_col(fill = "cornflowerblue", alpha = .9)+
  coord_flip() +
  labs(
    title = "Revenue by State",
    x = "State", 
    y="Revenue"
  ) +
  theme_minimal(base_size = 14) +
  theme(plot.title.position = "plot",
        panel.grid.major.y = element_blank(),
        panel.grid.major.x = element_line(color = "#A9A9A9", size = .6),
        panel.grid.minor.x = element_blank())  ## something appears to be going on with hawaii...

Disabilities by State

dis_state_sums <- dis_dat %>% 
  group_by(STATENAME) %>% 
  summarize(idea_tot = sum(IDEA_COUNT, na.rm = TRUE))


dis_state_sums %>% 
  ggplot(.,aes(fct_reorder(STATENAME, idea_tot), idea_tot))+
  geom_col(fill = "cornflowerblue", alpha = .9)+
  coord_flip() +
  labs(
    title = "Students with Disabilities by State",
    x = "State", 
    y="Students with Disabilities"
  ) +
  theme_minimal(base_size = 14) +
  theme(plot.title.position = "plot",
        panel.grid.major.y = element_blank(),
        panel.grid.major.x = element_line(color = "#A9A9A9", size = .6),
        panel.grid.minor.x = element_blank())  

#SpEd spending per student
fiscal_acgr1 <- inner_join(raw_fisc_2017, acgr_lea, by = "LEAID") %>% mutate(SpedPerStudent=C05+C15/CWD_COHORT) %>%  mutate(PerCWD=CWD_COHORT/ALL_COHORT) %>% select(C05, C15, SpedPerStudent, STNAME, CWD_RATE, CWD_COHORT, LEAID, ALL_COHORT, ALL_RATE, PerCWD) %>% filter(LEAID!="1500030")

fiscal_acgr1 <- fiscal_acgr1 %>% mutate(numCWDGradRate=recode(CWD_RATE,
                                        "1"= 1, "2"= 2, "3"= 3, "4"= 4, "5"= 5, "6"= 6, "7"= 7, "8"= 8, "9"= 9, "10"= 10, "11"= 11, "12"= 12, "13"= 13, "14"= 14, "15"= 15, "16"= 16, "17"= 17, "18"= 18, "19"= 19, "20"= 20, "21"= 21, "22"= 22, "23"= 23, "24"= 24, "25"= 25, "26"= 26, "27"= 27, "28"= 28, "29"= 29, "30"= 30, "31"= 31, "32"= 32, "33"= 33, "34"= 34, "35"= 35, "36"= 36, "37"= 37, "38"= 38, "39"= 39, "40"= 40, "41"= 41, "42"= 42, "43"= 43, "44"= 44, "45"= 45, "46"= 46, "47"= 47, "48"= 48, "49"= 49, "50"= 50, "51"= 51, "52"= 52, "53"= 53, "54"= 54, "55"= 55, "56"= 56, "57"= 57, "58"= 58, "59"= 59, "60"= 60, "61"= 61, "62"= 62, "63"= 63, "64"= 64, "65"= 65, "66"= 66, "67"= 67, "68"= 68, "69"= 69, "70"= 70, "71"= 71, "72"= 72, "73"= 73, "74"= 74, "75"= 75, "76"= 76, "77"= 77, "78"= 78, "79"= 79, "80"= 80, "81"= 81, "82"= 82, "83"= 83, "84"= 84, "85"= 85, "86"= 86, "87"= 87, "88"= 88, "89"= 89, "90"= 90, "91"= 91, "92"= 92, "93"= 93, "94"= 94, "95"= 95, "96"= 96, "97"= 97, "98"= 98, "99"= 99, "100"= 100,                                    "70-79" = 74.5,
                                        "40-49"= 44.5,
                                        "60-69"= 64.5,
                                        "60-79"= 69.5,
                                        "50-59"= 54.5,
                                        "LE5"= 2.5,
                                        "10-14"= 12,
                                        "60-64"= 62,
                                        "21-39"= 30,
                                        "30-39"= 34.5,
                                        "55-59"= 57,
                                        "LE10"= 5,
                                        "35-39"= 37,
                                        "40-44"= 42,
                                        "40-59"= 49.5,
                                        "50-54"= 52,
                                        "20-24"= 22,
                                        "15-19"= 17,
                                        "6-9"= 7.5,
                                        "11-19"= 15,
                                        "80-89"= 84.5,
                                        "70-74"= 72,
                                        "65-69"= 67,
                                        "75-79"= 77,
                                        "80-84"= 82,
                                        "85-89"= 87,
                                        "45-49"= 47,
                                        "90-94"= 92,
                                        "25-29"= 27,
                                        "GE90"= 95,
                                        "GE95"= 97.5,
                                        "20-29"= 24.5,
                                        "30-34"= 32))
## Warning: Unreplaced values treated as NA as `.x` is not compatible.
fiscal_acgr1 <- fiscal_acgr1 %>% mutate(numTotalGradRate=recode(ALL_RATE,
                                                                 "1"= 1, "2"= 2, "3"= 3, "4"= 4, "5"= 5, "6"= 6, "7"= 7, "8"= 8, "9"= 9, "10"= 10, "11"= 11, "12"= 12, "13"= 13, "14"= 14, "15"= 15, "16"= 16, "17"= 17, "18"= 18, "19"= 19, "20"= 20, "21"= 21, "22"= 22, "23"= 23, "24"= 24, "25"= 25, "26"= 26, "27"= 27, "28"= 28, "29"= 29, "30"= 30, "31"= 31, "32"= 32, "33"= 33, "34"= 34, "35"= 35, "36"= 36, "37"= 37, "38"= 38, "39"= 39, "40"= 40, "41"= 41, "42"= 42, "43"= 43, "44"= 44, "45"= 45, "46"= 46, "47"= 47, "48"= 48, "49"= 49, "50"= 50, "51"= 51, "52"= 52, "53"= 53, "54"= 54, "55"= 55, "56"= 56, "57"= 57, "58"= 58, "59"= 59, "60"= 60, "61"= 61, "62"= 62, "63"= 63, "64"= 64, "65"= 65, "66"= 66, "67"= 67, "68"= 68, "69"= 69, "70"= 70, "71"= 71, "72"= 72, "73"= 73, "74"= 74, "75"= 75, "76"= 76, "77"= 77, "78"= 78, "79"= 79, "80"= 80, "81"= 81, "82"= 82, "83"= 83, "84"= 84, "85"= 85, "86"= 86, "87"= 87, "88"= 88, "89"= 89, "90"= 90, "91"= 91, "92"= 92, "93"= 93, "94"= 94, "95"= 95, "96"= 96, "97"= 97, "98"= 98, "99"= 99, "100"= 100,                       
                                        "70-79" = 74.5,
                                        "40-49"= 44.5,
                                        "60-69"= 64.5,
                                        "60-79"= 69.5,
                                        "50-59"= 54.5,
                                        "LE5"= 2.5,
                                        "10-14"= 12,
                                        "60-64"= 62,
                                        "21-39"= 30,
                                        "30-39"= 34.5,
                                        "55-59"= 57,
                                        "LE10"= 5,
                                        "35-39"= 37,
                                        "40-44"= 42,
                                        "40-59"= 49.5,
                                        "50-54"= 52,
                                        "20-24"= 22,
                                        "15-19"= 17,
                                        "6-9"= 7.5,
                                        "11-19"= 15,
                                        "80-89"= 84.5,
                                        "70-74"= 72,
                                        "65-69"= 67,
                                        "75-79"= 77,
                                        "80-84"= 82,
                                        "85-89"= 87,
                                        "45-49"= 47,
                                        "90-94"= 92,
                                        "25-29"= 27,
                                        "GE90"= 95,
                                        "GE95"= 97.5,
                                        "20-29"= 24.5,
                                        "30-34"= 32))
## Warning: Unreplaced values treated as NA as `.x` is not compatible.
fiscal_acgr1 <- fiscal_acgr1 %>% mutate(GE50 = case_when(numCWDGradRate>50 | numCWDGradRate=="GE50" ~ 1, numCWDGradRate<50 | numCWDGradRate=="LT50"~0))


fiscal_acgr1 %>%  filter(abs(scale(SpedPerStudent))<=1.65) %>%ggplot(., aes(x=SpedPerStudent, y=GE50)) + 
  geom_point(alpha=.5) +
  stat_smooth(method="glm", se=TRUE, method.args = list(family=binomial)) + 
  labs(
    title = "Likelihood of a >50% CWD Cohort Graduation Rate vs Sped Revenue per Student",
    x = "Avg. Sped Revenue", 
    y="Likelihood"
  )+
  theme_minimal()
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 6265 rows containing non-finite values (stat_smooth).
## Warning: Removed 6265 rows containing missing values (geom_point).

fiscal_acgr1 <- fiscal_acgr1 %>% filter(!is.na(numTotalGradRate))
fiscal_acgr1 <- fiscal_acgr1 %>% filter(!is.na(numCWDGradRate))
fiscal_acgr1 <- fiscal_acgr1 %>% mutate(CWD_GRAD_COHORT= numCWDGradRate*CWD_COHORT*.01, NONCWD_GRAD_COHORT= (numTotalGradRate*ALL_COHORT*.01)-(numCWDGradRate*CWD_COHORT*.01))

stateLevel <- fiscal_acgr1 %>% group_by(STNAME) %>% summarize(CWD_COHORT=mean(CWD_COHORT, na.rm=TRUE), ALL_COHORT=mean(ALL_COHORT), SpedRev=mean(C05+C15, na.rm=TRUE), NONCWD_GRAD_COHORT=mean(NONCWD_GRAD_COHORT), CWD_GRAD_COHORT=mean(CWD_GRAD_COHORT)) %>% mutate(CWD_RATE=CWD_COHORT/ALL_COHORT) %>% mutate(nonCWD=ALL_COHORT-CWD_COHORT) %>% mutate(SpedPerStudent=SpedRev/CWD_COHORT, CWD_GRAD_RATE = CWD_GRAD_COHORT/CWD_COHORT, NON_CWD_GRAD_RATE=NONCWD_GRAD_COHORT/(ALL_COHORT-CWD_COHORT))


fiscal_acgr2 <-fiscal_acgr1%>% filter(is.numeric(SpedPerStudent))%>% filter(!is.na(SpedPerStudent)) %>% filter(SpedPerStudent>10) %>% group_by(STNAME) %>% summarize(StateSpedPerStudent=mean(SpedPerStudent))



fiscal_acgr2 %>% ggplot(., aes(x=StateSpedPerStudent, y=reorder(STNAME, StateSpedPerStudent))) +
  geom_point(size=2, shape=23)+
  geom_smooth(method=lm, se=FALSE)+
  labs(
    title = "State Avg Sped Revenue per CWD Student",
    x = "Avg. Sped Revenue", 
    y="State"
  )+
  theme_minimal()
## `geom_smooth()` using formula 'y ~ x'

fiscal_acgr1 %>% filter(abs(scale(SpedPerStudent))<=1.96) %>% 
ggplot(., aes(x=SpedPerStudent, y=numCWDGradRate-numTotalGradRate)) +
  geom_point(size=2, shape=23)+
  geom_smooth(method=lm, se=TRUE)+
  labs(
    title = "Difference in Graduation Rates of CWD - Non-CWD vs Avg Sped Revenue per CWD Student",
    x = "Avg. Sped Revenue", 
    y="Difference in Graduaion Rate, CWD-Non-CWD"
  )+
  theme_minimal()
## `geom_smooth()` using formula 'y ~ x'

stateLevel2 <- stateLevel %>% pivot_longer(., cols=c("nonCWD","CWD_COHORT"), values_to="count")

 stateLevel2%>% 
   ggplot(.,aes(x=reorder(STNAME, count), y=count, fill = name))+
  geom_col(position = "stack")+
  coord_flip() +
  labs(
    title = "Students with and without Disabilities by State",
    x = "State", 
    y="Students with Disabilities"
  )

stateLevel%>% 
   ggplot(.,aes(x=reorder(STNAME,CWD_RATE), y=CWD_RATE))+
  geom_col(position = "stack")+
  coord_flip() +
  labs(
    title = "Students with Disability Rates by State",
    x = "State", 
    y="Percent of Students with Disabilities"
  )

stateLevel%>% 
   ggplot(.,aes(x=reorder(STNAME,CWD_RATE), y=SpedPerStudent))+
  geom_col(position = "stack")+
  coord_flip() +
  labs(
    title = "SpEd Revenue per Student by State",
    x = "States in Order of Disability Rates, Descending", 
    y="Avg Special Ed Revenue per Student"
  )

stateLevel%>% 
   ggplot(.,aes(x=reorder(STNAME,CWD_GRAD_RATE-NON_CWD_GRAD_RATE), y=CWD_GRAD_RATE-NON_CWD_GRAD_RATE))+
  geom_col(position = "stack")+
  coord_flip() +
  labs(
    title = "Differences in CWD/Non-CWD Grad Rates by State",
    x = "State in Order of Disability Rates, Descending", 
    y="Differences in Grad Rates, Disability minus No Disabilties"
  )

library(albersusa)
library(colorspace)
us <- usa_sf()

stateLevel_subset <- stateLevel %>%
  select("name"="STNAME", CWD_RATE, SpedPerStudent,CWD_GRAD_RATE,NON_CWD_GRAD_RATE)

stateLevel_geo <- inner_join(us, stateLevel_subset) 
## Joining, by = "name"old-style crs object detected; please recreate object with a recent sf::st_crs()
ggplot(stateLevel_geo) +
  geom_sf(aes(fill=CWD_RATE)) +
  scale_fill_continuous_sequential(palette = "Heat") + 
  labs(fill = "Percent of Students with Disabilities",
       title = "Students with Disability Rates by State") +
  theme_minimal(base_size = 12) +
  theme(plot.title.position = "plot",
        legend.position = "bottom",
        legend.key.size = unit(1, 'cm'),
        legend.key.height = unit(.5,"cm"),
        axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6))

ggplot(stateLevel_geo) +
  geom_sf(aes(fill=SpedPerStudent)) +
  scale_fill_continuous_sequential(palette = "Heat") + 
  labs(fill = "Avg Special Ed Revenue per Student",
       title = "SpEd Revenue per Student by State") +
  theme_minimal(base_size = 12) +
  theme(plot.title.position = "plot",
        legend.position = "bottom",
        legend.key.size = unit(1.2, 'cm'),
        legend.key.height = unit(.5,"cm"),
        axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6))

ggplot(stateLevel_geo) +
  geom_sf(aes(fill=CWD_GRAD_RATE-NON_CWD_GRAD_RATE)) +
  scale_fill_continuous_sequential(palette = "Heat") + 
  labs(fill = "Differences in Grad Rates\n(Disability - No Disabilties)",
       title = "Differences in CWD/Non-CWD Grad Rates by State") +
  theme_minimal(base_size = 12) +
  theme(plot.title.position = "plot",
        legend.position = "bottom",
        legend.key.size = unit(1.2, 'cm'),
        legend.key.height = unit(.5,"cm"),
        axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6))

County Level Data Prep

library(edbuildmapr)
## 
## Attaching package: 'edbuildmapr'
## The following object is masked from 'package:ggplot2':
## 
##     borders
sd17 <- sd_shapepull(data_year = "2017", with_data = TRUE)
## Reading layer `shapefile_1718_4269' from data source 
##   `/private/var/folders/74/cwwvnx_977bd5z2s7xgk4_nm0000gn/T/RtmpsuuPSh/file13c04cb02629/shapefile_1718_4269.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 13330 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.1686 ymin: 18.91382 xmax: 179.7487 ymax: 71.38881
## Geodetic CRS:  NAD83
## NOTE::This shapefile has been simplified to make analysis quicker. For final vizualizations, please use the unsimplified shapefiles available through NCES.
sd17$year <- as.numeric(sd17$year)
sd17$leaid <- sd17$GEOID

county <- counties_sf() %>% 
  mutate(County = paste0(name, " ",lsad)) %>% 
  select(County, geometry, state_name = state)


dis_dat_sub <- dis_dat %>% 
  select(state_name = STATENAME, LEAID, LEA_NAME, YEAR , IDEA_COUNT) %>% 
  janitor::clean_names()
dis_dat_sub$state_name <- str_to_title(dis_dat_sub$state_name)

fiscal_temp <- inner_join(raw_fisc_2017, acgr_lea, by = "LEAID") %>% mutate(SpedPerStudent=C05+C15/CWD_COHORT) %>%  mutate(PerCWD=CWD_COHORT/ALL_COHORT) %>% select(leaid = LEAID, C05, C15, SpedPerStudent, STNAME, CWD_RATE, CWD_COHORT, LEAID, ALL_COHORT, ALL_RATE, PerCWD) %>% filter(leaid!="1500030")


fiscal_disability <- inner_join(fiscal_temp, dis_dat_sub)
## Joining, by = "leaid"
district_geo <- left_join(sd17, fiscal_disability)
## Joining, by = c("year", "leaid")
to_sum <- district_geo
st_geometry(to_sum) <- NULL
county_sums <- to_sum %>% 
  group_by(County, state_name) %>% 
  summarize(mean_sped_stud = mean(SpedPerStudent, na.rm = TRUE), 
            tot_idea = sum(idea_count))
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.
county_map <- merge(county_sums, county, by = c("County", "state_name"), all = F)
## old-style crs object detected; please recreate object with a recent sf::st_crs()

Bad County Level Plots

#this is ugly - some of these numbers are impossibly high...need to recheck calculations
county_map %>% 
  filter(mean_sped_stud >0) %>% 
ggplot() +
  geom_sf(aes(fill=mean_sped_stud, geometry = geometry)) +
  scale_fill_continuous_sequential(palette = "Heat") + 
  labs(fill = "Percent of Students with Disabilities",
       title = "Students with Disability Rates by State") +
  theme_minimal(base_size = 12) +
  theme(plot.title.position = "plot",
        legend.position = "bottom",
        legend.key.size = unit(1, 'cm'),
        legend.key.height = unit(.5,"cm"),
        axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6))

# maybe just oregon
county_map %>% 
  filter(state_name =="Oregon") %>% 
  ggplot()+
    geom_sf(aes(fill=mean_sped_stud, geometry = geometry)) +
  scale_fill_continuous_sequential(palette = "Heat") + 
  labs(title = "Mean Special Ed. Spending per Student", 
       fill = "") +
  theme_minimal(base_size = 12) +
  theme(plot.title.position = "plot",
        legend.position = "bottom",
        legend.key.size = unit(1, 'cm'),
        legend.key.height = unit(.5,"cm"),
        axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6))

county_map %>% 
  filter(state_name =="Oregon") %>% 
  ggplot()+
    geom_sf(aes(fill=tot_idea, geometry = geometry)) +
  scale_fill_continuous_sequential(palette = "Heat") + 
  labs(title = "Total Sped Students", 
       fill = "") +
  theme_minimal(base_size = 12) +
  theme(plot.title.position = "plot",
        legend.position = "bottom",
        legend.key.size = unit(1, 'cm'),
        legend.key.height = unit(.5,"cm"),
        axis.text.y = element_text(size = 6),
        axis.text.x = element_text(size = 6))

District Level Plots

ore_district <- district_geo %>% 
  filter(state_name=="Oregon") %>% 
  select(idea_count, idea_rev_fed = C15, geometry)  

plot(ore_district, border = "light gray", lwd = 0.5)